package com.rx.bean;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

@Slf4j
public class ReadProperties {
    public static Properties getProperties(String path) throws FileNotFoundException, IOException {
        Properties props = null;
        log.debug("文件：" + path);
        File file = new File(path);
        if(file.exists() && file.isFile()){
            props = new Properties();
            props.load(new FileInputStream(file));
        }else{
            System.out.println(file.toString() + "不存在！");
            log.debug(file.toString() + "不存在！");
        }
        return props;
    }

    public static String getValue(Properties props,String key,String encod){
        String result = "";
        String en;
        String localEN = System.getProperty("file.encoding");
        if(encod !=null && !encod.equals("") ){
            en = encod;
        }else{
            en = localEN;
        }
        try {
            key = new String(key.getBytes(en),"ISO-8859-1");
            result = props.getProperty(key);
            if(!result.equals("")){
                result = new String(result.getBytes("ISO-8859-1"),en);
            }
        } catch (Exception e) {

        }finally{
            if (result == null){
                log.debug(key + "为空");
                result = "";
            }
            return result;
        }
    }

    public static String getValue(Properties props,String key){
        return getValue(props, key, "");
    }

    public static void loadConfig(RxMainConfigBean rxMainConfigBean){

        Properties props = null;
        try {
            props = getProperties( rxMainConfigBean.getFilePath()+"ad.properties");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;

        } catch (IOException e) {
            e.printStackTrace();
            return;
        }


        rxMainConfigBean.setAdFileName(getValue(props, "spring.app.adFileName"));
        rxMainConfigBean.setAdShow(getValue(props, "spring.app.show_ad"));
        rxMainConfigBean.setAppApk(getValue(props, "spring.app.apkFileName"));
        rxMainConfigBean.setAppVersion(getValue(props, "spring.app.appVersion"));
    }

}
